﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Collections;
using System.IO;
using DeviceSDK.LED.NuoWa.JT100;
using BaseHelper.Text;
using BaseHelper;
using BaseHelper.Base;

namespace DeviceSDK.LED.NuoWa
{
    class NuoWa_JT100GY
    {
        private static SortedList<string, string> slIPList = new SortedList<string, string>();
        private static string ipFileName="中海转诺瓦.txt";

        //获取错误代码
        public static string Get_Error(int ret)
        {
            switch (ret)
            {
                case 0:
                    return "操作成功！";

                case 1:
                    return "操作失败！";

                case 2:
                    return "发送节目单失败！";

                case 3:
                    return "保存节目单到磁盘失败！";

                case 4:
                    return "有素材文件格式不支持！";

                case 5:
                    return "素材内容为空！";

                case 6:
                    return "没有检索到对应关系<中海转诺瓦.txt>！";

                case -1:
                    return "操作失败！";

                default:
                    return "未知错误！";
            }
        }


        public static JT100_SDK Connect(string IP)
        {
            if (slIPList.Count==0)
            {
                string ipListPath = SysHelper.AppPath + "\\" + ipFileName;
                if (File.Exists(ipListPath))
                {
                    foreach (string r in File.ReadAllLines(ipListPath))
                    {
                        if (r.Contains("="))
                        {
                            //52.31.1.1=53.32.1.4:8080
                            string[] sp = r.Split('=');
                            slIPList.Add(sp[0], sp[1]);
                        }
                    }
                }
                else
                {
                    LogHelper.Debug("诺瓦JT100GY", "未找到" + ipListPath);
                }
            }

            JT100_SDK jt100 = new JT100_SDK();
            if (jt100.Connect(IP))
            {
                return jt100;
            }
            return null;
        }

        //发送-节目
        public static LEDSendResult Send_Programs(bool IsTempPlaybill, string IP, int Width, int Height, PlaybillProgramInfo[] Programs, bool isCheckResult=false)
        {
            JT100_SDK jt100 = Connect(IP);
            LEDSendResult result = new LEDSendResult(isCheckResult);
            if (jt100 == null)
            {
                result.RetCode = 1;
                return result;
            }
            PlayBill pb=new PlayBill();

            //SortedList<int, List<PlaybillProgramInfo>> slScreenProgram = new SortedList<int, List<PlaybillProgramInfo>>();
            //foreach (PlaybillProgramInfo p in Programs)
            //{
            //    if (!slScreenProgram.ContainsKey(p.ScreenAreaID))
            //    {
            //        slScreenProgram.Add(p.ScreenAreaID,new List<PlaybillProgramInfo>());
            //    }
            //    slScreenProgram[p.ScreenAreaID].Add(p);
            //}

            //foreach(int ScreenID in slScreenProgram.Keys)
            //{
            //    List<PlaybillProgramInfo> plist = slScreenProgram[ScreenID];
            //    int playTime = 0;
            //    foreach (PlaybillProgramInfo pinfo in plist)
            //    {
            //        playTime += pinfo.PlayTime;
            //    }
            //    pb.AddPage(playTime*10,1,1);   // pinfo.Affect == 0 ? 1 : 255, pinfo.Affect  //pinfo.PlayTime * 10


            //2020.6.18 临时代码

            if (slIPList.ContainsKey(IP))
            {
                string[] rip = slIPList[IP].Split(':');
                LEDHelper led = new LEDHelper();
                string content = "";
                int ret2 = led.Get_PlaybillContent("ZHONGHAI_V1", rip[0], Convert.ToInt32(rip[1]), ref content);
                if (ret2 == 0 && content.Trim().Length > 0)
                {
                    string[] texts = content.Contains("\r\n") ? content.Replace("\r\n", "|").Split('|') : new string[] { content };
                    foreach (string text in texts)
                    {
                        if (text.Trim().Length == 0)
                            continue;
                        //    0            1      2             3         4    5  6
                        // p.PlayTime,p.FontName,p.FontSize,p.FontColor,p.X,p.Y,p.Text
                        string[] sp = text.Split(',');
                        pb.AddPage(Convert.ToInt32(sp[0]) / 10, 0, 0);
                        pb.AddString(sp[6].Trim(), 0, 0, Width, Height, sp[1], Convert.ToInt32(sp[2]), 0, 2, 2, 0, 0, sp[3],  0, 0, 1, Convert.ToInt32(sp[0]) / 10);
                    }
                }
            }
            else
            {
                LogHelper.Debug("诺瓦JT100GY", "Send_Programs->没有对应关系" + IP);
                jt100.Close();
                result.RetCode = 6;
                return result;
            }

            bool IsOnlyPage = false;
            foreach (PlaybillProgramInfo pinfo in Programs)
            {
                //PlaybillProgramInfo pinfo =dic.Value  Programs[i];

                //pinfo.ScreenAreaID == 0;
                if (pinfo.ScreenAreaID == 0)
                {
                    IsOnlyPage = false;
                    pb.AddPage(pinfo.PlayTime * 10, pinfo.IsEffectRand == 0 ? 1 : 255, pinfo.IsEffectRand);  //pinfo.PlayTime * 10
                }
                else
                {
                    if (!IsOnlyPage)
                    {
                        int PlayTime = 0;
                        foreach (PlaybillProgramInfo p in Programs)
                        {
                            if (p.ScreenAreaID != 0)
                                PlayTime += p.PlayTime;
                        }
                        pb.AddPage(PlayTime * 10, 1, 1);  //pinfo.PlayTime * 10
                        IsOnlyPage = true;
                    }
                }

                switch (pinfo.Type)
                {
                    case "picture":
                        if (!File.Exists(pinfo.FilePath))
                            continue;
                        pb.AddImage(pinfo.FilePath, pinfo.PlayTime * 10, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height);
                        break;

                    case "video":
                        if (!File.Exists(pinfo.FilePath))
                        {
                            pb.RemoveLastPage();
                            continue;
                        }
                        pb.AddVideo(pinfo.FilePath, pinfo.PlayTime * 10, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height);
                        break;

                    case "rtf":
                        pb.RemoveLastPage();
                        break;

                    case "text":
                        pinfo.Text = pinfo.Text.Replace("\n", "\r\n").Replace("\r\r", "\r");
                        if (pinfo.Text == "")
                        {
                            pb.RemoveLastPage();
                            continue;
                        }
                        pb.AddString(pinfo.Text, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height
                            , pinfo.FontName, pinfo.FontSize, pinfo.FontIsBold ? 1 : 0, pinfo.PositionHor, pinfo.PositionVer, pinfo.LineSpace, 0, pinfo.FontColor, pinfo.IsEffectRand, pinfo.IsEffectRand, 1, pinfo.PlayTime * 10);
                        break;
                }
            }

            //}
            if (pb.Count == 0)
            {
                jt100.Close();
                result.RetCode = 5;
                return result;
            }
                
            //}

            int playbillId = IsTempPlaybill ? 2 : 1;
            int ret = 1;

  
            if (jt100.SendPlaybill(playbillId, pb))
            {
              
                if (jt100.SetPlaybill(playbillId))
                {
                    if (isCheckResult)
                    {
                        result.SendContent = pb.Create();
                        result.ReplayContent = jt100.GetPlaybillContent();  
                    }

                    jt100.CleanFile(CleanFileType.NoUse);
                    ret = 0;
                }
                else
                    ret = 1;
            }
            else
                ret = 2;


            if (!IsTempPlaybill)
            {
                //jt100.SetVirtualCheck(VirtualCheckState.Close);
                //jt100.SetVirtualCheck(VirtualCheckState.Open, Config.Release_Play_TimeOut * 60, 1);
            }

            jt100.Close();
            result.RetCode = ret;
            return result;

        }
    }
}
